home *** CD-ROM | disk | FTP | other *** search
/ Varios Español / Varios Español.iso / DBASE5 / CUA_SAMP.ZIP / TMPNAME.PRG < prev    next >
Text File  |  1994-10-12  |  3KB  |  105 lines

  1. FUNCTION TmpName    && Returns a pseudo-random file root name
  2. PARAMETER pc_ext
  3. *--------------------------------------------------------------------
  4. * NAME
  5. *   TmpName - Returns a pseudo-random file root name.
  6. *
  7. * SYNOPSIS
  8. *   TmpName( [.ext] )
  9. *
  10. * DESCRIPTION
  11. *   TmpName() returns an pseudo-random string of
  12. *   digits suitable for use as a temporary file name.
  13. *   Eight digits (sometimes fewer) are returned.
  14. *   Successive calls to TmpName() can be used to
  15. *   generate a series of unique file names.
  16. *
  17. *   An optional file extension can be passed as an
  18. *   argument.  If this is done, TmpName will make
  19. *   sure that the file name it returns does not already
  20. *   exist within the current dBASE path setting.
  21. *
  22. * PARAMETERS
  23. *   pc_ext - optional file name extension.  May optionally
  24. *   start with a ".", followed by up to three characters.
  25. *   (Note that some characters are not allowed in file names,
  26. *    depending on the specific operating system in use.)
  27. *
  28. * EXAMPLE
  29. *
  30. *   lc_tmpfile = TmpName(".TMP")
  31. *   * Possible return value: "87113336.TMP"
  32. *   USE master
  33. *   COPY TO (lc_tmpfile)
  34. *   * The file "87113336.TMP" would now exist
  35. *
  36. * LIMITATIONS
  37. *   If TmpName() is used without the extension
  38. *   parameter, the FILE() function can be used to
  39. *   make certain that a created file name does not
  40. *   already exist.
  41. *
  42. *   TmpName() assumes the extension argument has
  43. *   only characters allowed in filenames.
  44. *
  45. *   Note also that leading 0's will not be returned.
  46. *   If you desire exactly eight digits, this line:
  47. *     TRANSFORM( RAND(-1) * 100000000, "@L 99999999" )
  48. *   returns a random string of digits that is always
  49. *   eight characters long.
  50. *
  51. * SEE ALSO:
  52. *   RAND(), FILE()
  53. *
  54. *--------------------------------------------------------------------
  55.     PRIVATE lc_env, lc_ext, lc_prefix, lc_root, lc_slash, ;
  56.     ll_err, lh_chkit, cID
  57.  
  58.     IF LEFT( OS(), 3 ) = "DOS"
  59.         lc_slash = "\"
  60.     ELSE
  61.         lc_slash = "/"
  62.     ENDIF
  63.  
  64.     lh_chkit = 0
  65.  
  66.     lc_env = ""
  67.     lc_prefix = ""
  68.     cID = ID()
  69.     IF .NOT. ISBLANK( m->cID )
  70.         IF LEN( m->cID ) = 1
  71.             cID = m->cID + "_"
  72.         ENDIF
  73.         lc_Prefix = LEFT( m->cID, 2 ) + "_"
  74.     ELSE
  75.         lc_Prefix = "XT_"
  76.     ENDIF
  77.  
  78.     IF PCOUNT() = 0
  79.         lc_Root = m->lc_prefix + LTRIM( STR( RAND( -1 ) * 100000000, 8 ) )
  80.         lc_root = TRIM( LEFT( m->lc_Root + SPACE(8), 8 ) )
  81.         RETURN( m->lc_root )
  82.     ELSE
  83.  
  84.         IF .NOT. "." $ m->pc_ext
  85.             lc_ext = "." + m->pc_ext
  86.         ELSE
  87.             lc_ext = m->pc_ext
  88.         ENDIF
  89.  
  90.         lc_ext = SUBSTR(m->lc_ext, 1, 4)
  91.  
  92.         DO WHILE .T.
  93.             lc_Root = m->lc_prefix + LTRIM( STR( RAND( -1 ) * 100000000, 8 ) )
  94.             lc_root = TRIM( LEFT( m->lc_Root + SPACE(8), 8 ) )
  95.  
  96.             IF .NOT. FILE( m->lc_root + m->lc_ext )
  97.                 RETURN( m->lc_root + m->lc_ext )
  98.             ENDIF
  99.  
  100.         ENDDO
  101.  
  102.     ENDIF
  103. *-- EOF: TmpName( [.ext] )
  104.  
  105.